<?php

namespace common\service;

use common\exception\LogicException;
use common\service\task_list_sorter\AssetTaskListSorter;
use common\service\task_list_sorter\ShotTaskListSorter;
use support\ErrorCode;

/**
 * 任务列表排序服务
 */
class TaskListSorterService
{
    protected TaskListSorterInterface $handler;

    /**
     * 构造方法
     * @param TaskListSorterInterface $handler
     */
    protected function __construct(TaskListSorterInterface $handler)
    {
        $this->handler = $handler;
    }

    /**
     * 获得一个任务排序器
     * @param $moduleCode
     * @return static
     */
    public static function getInstance($moduleCode)
    {
        switch ($moduleCode) {
            case 'asset':
            case 'level':
            case 'design':
                return new static(new AssetTaskListSorter());

            case 'shot':
            case 'sequence':
            case 'storyboard':
                return new static(new ShotTaskListSorter());
            default:
                throw new LogicException($moduleCode . ' is not support', ErrorCode::MODULE_NOT_SUPPORT);
        }
    }

    /**
     * 进行排序
     * @param $taskList
     * @return void
     */
    public function sort(&$taskList)
    {
        $this->handler->sortTaskList($taskList);
    }
}
